package com.monkeyboy.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.MongoTransactionManager;

/**
 * @Author Gavin
 * @date 2021.01.22 15:11
 */
@Configuration
public class Config {
    @Bean
    //除了利用ConditionOnProperty开启事务之外还可以在启动类上面加@EnableTransactionManagement注解启用
    @ConditionalOnProperty(name = "spring.data.mongodb.transactionEnabled", havingValue = "true")
    MongoTransactionManager transactionManager(MongoDbFactory factory) {//高版本的已经抛弃了MongoDBFactory,使用MongoDatabaseFactory
        return new MongoTransactionManager(factory);
    }
    //注意：如果是多数据源的话需要指定事务管理器，比如mysql和Mongodb都需要事务的时候
    //mongodb的事务只支持副本集，分片的4.0以上的才支持
    // @Transactional(value = "mongoDBTransactionManager")
}
